package cn.demoncat.dynamic_table;

import cn.demoncat.dynamic_table.mapper.InterfaceInLogMapper;
import cn.demoncat.util.lang.constant.DateConstant;
import cn.demoncat.util.mybatis.plus.MybatisPlusUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 系统初始化
 *
 * @author 延晓磊
 * @since 2020年10月16日
 */
@Component
public class InitHandler implements CommandLineRunner {

    @Resource
    private InterfaceInLogMapper interfaceInLogMapper;

    @Override
    public void run(String... args) {
        // 当前日期
        String date = LocalDateTime.now().format(DateConstant.MONTH_FORMAT_NUM);
        // 切换动态表
        MybatisPlusUtil.addDynamicTable("interface_in_log", date);
        try {
            // 操作动态表
            interfaceInLogMapper.selectCount(Wrappers.emptyWrapper());
        }catch (Exception e){
            // 语法错误，新建动态表
            interfaceInLogMapper.createNewTable(date);
        }
    }


}
